//
//  VisitorView.swift
//  weibo个人项目
//
//  Created by 童冀 on 9/8/15.
//  Copyright © 2015 童冀. All rights reserved.
//

import UIKit
protocol VisitorViewDelegate:NSObjectProtocol{
    func loginBtnWillClick()
    func registerBtnWillClick()
}

class VisitorView: UIView {
    weak var delegate:VisitorViewDelegate?
    
    func setupVisitorInfo(isHome:Bool, imageName:String, message:String)
    {
        iconView.hidden = !isHome
        homeIcon.image = UIImage(named: imageName)
        messageLabel.text = message
        if isHome {
            startAnimation()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        addSubview(iconView)
        addSubview(maskBGView)
        addSubview(homeIcon)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        homeIcon.xmg_AlignInner(type: XMG_AlignType.Center, referView: self, size: nil)
        messageLabel.xmg_AlignInner(type: XMG_AlignType.BottomCenter, referView: iconView, size: nil)
        let widthCons = NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224)
        addConstraint(widthCons)
        registerBtn.xmg_AlignInner(type: XMG_AlignType.BottomLeft, referView: messageLabel, size: CGSize(width: 100, height: 30), offset:CGPoint(x: 0, y: 20))
        loginBtn.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: messageLabel, size: CGSize(width: 100, height: 30), offset:CGPoint(x: 0, y: 20))
        maskBGView.xmg_Fill(self)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20.0
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    func loginBtnClick (){
        delegate?.loginBtnWillClick()
    }
    
    func registerBtnClick (){
        delegate?.registerBtnWillClick()
    }
    
    // MARK: - 懒加载控件
    lazy private var iconView: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return iv
    }()
    
    private lazy var homeIcon: UIImageView = {
        let hi = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return hi
    }()
    
    private lazy var messageLabel: UILabel = {
        let ml = UILabel()
        ml.numberOfLines = 0
        ml.textColor = UIColor.darkTextColor()
        ml.text = "asdlfkajsflajfal;af"
        return ml
    }()
    
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        let rbtn = UIButton()
        rbtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        rbtn.setTitle("注册", forState: UIControlState.Normal)
        rbtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        rbtn.addTarget(self, action: "registerBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return rbtn
    }()
    
    private lazy var maskBGView:UIImageView = {
        let iv = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return iv
    }()
}
